﻿namespace PMS.Client.FrmJobTitle
{
    using AdvancedDataGridView;
    using PMS.BLL;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class FrmSelect : Form
    {
        private IContainer components = null;
        private JobTitleBLL titleBLL = new JobTitleBLL();
        private DataGridViewTextBoxColumn titleID;
        private TreeGridColumn titleName;
        private JobTitleTypeBLL titleTypeBLL = new JobTitleTypeBLL();
        private TreeGridView treeGridView1;
        private DataGridViewTextBoxColumn typeID;

        public FrmSelect()
        {
            this.InitializeComponent();
            this.LoadJobTitle2();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmSelect));
            this.treeGridView1 = new TreeGridView();
            this.titleName = new TreeGridColumn();
            this.typeID = new DataGridViewTextBoxColumn();
            this.titleID = new DataGridViewTextBoxColumn();
            ((ISupportInitialize) this.treeGridView1).BeginInit();
            base.SuspendLayout();
            this.treeGridView1.AllowUserToAddRows = false;
            this.treeGridView1.AllowUserToDeleteRows = false;
            this.treeGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.treeGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            this.treeGridView1.BackgroundColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.treeGridView1.BorderStyle = BorderStyle.None;
            this.treeGridView1.Columns.AddRange(new DataGridViewColumn[] { this.titleName, this.typeID, this.titleID });
            this.treeGridView1.Dock = DockStyle.Fill;
            this.treeGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
            this.treeGridView1.EnableHeadersVisualStyles = false;
            this.treeGridView1.ImageList = null;
            this.treeGridView1.Location = new Point(0, 0);
            this.treeGridView1.MultiSelect = false;
            this.treeGridView1.Name = "treeGridView1";
            this.treeGridView1.RowHeadersVisible = false;
            this.treeGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.treeGridView1.Size = new Size(0x161, 0x1d3);
            this.treeGridView1.TabIndex = 5;
            this.treeGridView1.DoubleClick += new EventHandler(this.treeGridView1_DoubleClick);
            this.titleName.DefaultNodeImage = null;
            this.titleName.FillWeight = 80f;
            this.titleName.HeaderText = "职称列表";
            this.titleName.Name = "titleName";
            this.titleName.Resizable = DataGridViewTriState.True;
            this.titleName.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.typeID.HeaderText = "typeID";
            this.typeID.Name = "typeID";
            this.typeID.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.typeID.Visible = false;
            this.titleID.HeaderText = "titleID";
            this.titleID.Name = "titleID";
            this.titleID.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.titleID.Visible = false;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x161, 0x1d3);
            base.Controls.Add(this.treeGridView1);
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.Name = "FrmSelect";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "职称列表";
            ((ISupportInitialize) this.treeGridView1).EndInit();
            base.ResumeLayout(false);
        }

        private void LoadJobTitle2()
        {
            Font font = new Font(this.treeGridView1.DefaultCellStyle.Font, FontStyle.Bold);
            List<UserJobTitleType> list = this.titleTypeBLL.GetList();
            List<UserJobTitle> list2 = this.titleBLL.GetList();
            this.treeGridView1.Nodes.Clear();
            using (List<UserJobTitleType>.Enumerator enumerator = list.GetEnumerator())
            {
                Predicate<UserJobTitle> match = null;
                UserJobTitleType type;
                while (enumerator.MoveNext())
                {
                    type = enumerator.Current;
                    object[] values = new object[3];
                    values[0] = type.TypeName;
                    values[1] = type.TypeID;
                    TreeGridNode node = this.treeGridView1.Nodes.Add(values);
                    node.DefaultCellStyle.Font = font;
                    node.DefaultCellStyle.ForeColor = Color.White;
                    node.DefaultCellStyle.BackColor = SystemColors.Highlight;
                    node.Expand();
                    if (match == null)
                    {
                        match = f => f.TitleTypeID == type.TypeID;
                    }
                    List<UserJobTitle> list3 = list2.FindAll(match);
                    if (list3.Count > 0)
                    {
                        foreach (UserJobTitle title in list3)
                        {
                            node.Nodes.Add(new object[] { title.TitleName, title.TitleTypeID, title.TitleID }).DefaultCellStyle.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
                        }
                    }
                }
            }
        }

        private void treeGridView1_DoubleClick(object sender, EventArgs e)
        {
            if ((this.treeGridView1.SelectedRows.Count > 0) && (this.treeGridView1.SelectedRows[0].Cells["titleID"].Value != null))
            {
                this.JobTitle = this.treeGridView1.SelectedRows[0].Cells["titleName"].Value.ToString();
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
        }

        public string JobTitle { get; set; }
    }
}

